package com.sum.slike;

import java.util.List;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/26
 */
public abstract class BaseAnimationFrame implements AnimationFrame {

    private int x;
    private int y;
    private double currentTime;
    private boolean isRunnable;
    /**
     * 动画属性集合
     */
    protected List<Element> elements;
    private AnimationEndListener animationEndListener;
    /**
     * 动画执行时间
     */
    protected long duration;

    public BaseAnimationFrame(final long duration) {
        this.duration = duration;
    }

    @Override
    public boolean isRunning() {
        return isRunnable;
    }

    @Override
    public List<Element> nextFrame(final long interval) {
        currentTime += interval;
        if (currentTime >= duration) {
            isRunnable = false;
            if (animationEndListener != null) {
                animationEndListener.onAnimationEnd(this);
            }
        } else {
            for (Element element : elements) {
                element.evaluate(x, y, currentTime);
            }
        }
        return elements;
    }

    @Override
    public void setAnimationEndListener(final AnimationEndListener animationEndListener) {
        this.animationEndListener = animationEndListener;
    }

    @Override
    public void reset() {
        currentTime = 0;
        if (elements != null) {
            elements.clear();
        }
    }

    /**
     * 生成Element 集合列表
     * @param x x
     * @param y y
     * @param bitmapProvider bitmapProvider
     * @return List<Element>
     */
    protected abstract List<Element> generatedElements(int x, int y, BitmapProvider.Provider bitmapProvider);

    /**
     * 设置起始位置
     * @param x x
     * @param y y
     */
    protected void setStartPoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean onlyOne() {
        return false;
    }
}
